#include <direct.h>
#include <iostream>
#include <stdexcept>

#include "IBE_AnsysRstReader.h"
#include "IBE_Common.h"
#include "IBE_DataInformationAPI.h"

// ----------------------------------------------------------------------------
ibe_ansys::IBE_AnsysRstReader::IBE_AnsysRstReader( const char* file_name /*= ""*/ ) 
    : ibe_common::IBE_ObjectReader( file_name )
{
}

// ----------------------------------------------------------------------------
ibe_ansys::IBE_AnsysRstReader::~IBE_AnsysRstReader()
{
    this->InitData();
}

// ----------------------------------------------------------------------------
void ibe_ansys::IBE_AnsysRstReader::Initialize()
{
    this->Superclass::Initialize();
}

// ----------------------------------------------------------------------------
void ibe_ansys::IBE_AnsysRstReader::InitData()
{
    this->Superclass::InitData();

    this->FreePyObjects();
}

// ----------------------------------------------------------------------------
std::vector<ibe_common::IBE_FieldData*> ibe_ansys::IBE_AnsysRstReader::AnalyzeBaseArrayDataFromPythonInstance(
    PyObject* instance_py, const char* data_type )
{
    std::vector<ibe_common::IBE_FieldData*> fieldInfos;
    if( !instance_py )
    {
        return fieldInfos;
    }
    char* fun_name = "";
    if( !strcmp( data_type, "FieldData" ) )
    {
        fun_name = "GetFieldData";
    }
    else if( !strcmp( data_type, "PointData" ) )
    {
        fun_name = "GetPointData";
    }
    else // !strcmp( data_type, "CellData" )
    {
        fun_name = "GetCellData";
    }
    PyObject* get_attribute_data_py = PyObject_CallMethod( instance_py, fun_name, "", "" );
    if( get_attribute_data_py )
    {
        int64_t data_array_size = PyList_Size( get_attribute_data_py );
        fieldInfos.reserve( data_array_size );
        for( auto i = 0; i < data_array_size; i++ )
        {
            PyObject* array_dict = PyList_GetItem( get_attribute_data_py, i );
            ibe_common::IBE_FieldData* fieldInfo = this->AnalyzeArrayFromPythonDict( array_dict, data_type );
            if( fieldInfo )
            {
                fieldInfos.emplace_back( fieldInfo );
            }
        }
        this->pyObjects_.emplace_back( get_attribute_data_py );
    }
    return fieldInfos;
}

// ----------------------------------------------------------------------------
ibe_common::IBE_FieldData* ibe_ansys::IBE_AnsysRstReader::AnalyzeArrayFromPythonDict(
    PyObject* array_dict, const char* data_type )
{
    if( !array_dict || !PyDict_Check( array_dict ) )
    {
        return nullptr;
    }

    int fileLocation = ibe_common::IBE_FieldData::IBE_FIELD_LOCATION_NODE;
    if( !strcmp( data_type, "PointData" ) )
    {
        fileLocation = ibe_common::IBE_FieldData::IBE_FIELD_LOCATION_NODE;
    }
    else if( !strcmp( data_type, "CellData" ) )
    {
        fileLocation = ibe_common::IBE_FieldData::IBE_FIELD_LOCATION_CELL_CENTER;
    }
    else // !strcmp( data_type, "FieldData" )
    {
        return nullptr;
    }

    // 变量名
    PyObject *key_array_name = PyUnicode_FromString( "name" );
    this->pyObjects_.emplace_back( key_array_name );
    std::string array_name = PyUnicode_AsUTF8( PyDict_GetItem( array_dict, key_array_name ) );
    // 数据类型
    PyObject *key_data_type = PyUnicode_FromString( "data_type" );
    this->pyObjects_.emplace_back( key_data_type );
    int array_data_type = PyLong_AsLong( PyDict_GetItem( array_dict, key_data_type ) );
    // 分量数量
    PyObject *key_component_size = PyUnicode_FromString( "component_size" );
    this->pyObjects_.emplace_back( key_component_size );
    int component_size = PyLong_AsLong( PyDict_GetItem( array_dict, key_component_size ) );
    // 分量名
    PyObject *key_component_names = PyUnicode_FromString( "component_names" );
    this->pyObjects_.emplace_back( key_component_names );
    PyObject* list_component_names = PyDict_GetItem( array_dict, key_component_names );
    //int is_list = PyList_Check( list_component_names );
    int64_t num_of_component_names = PyList_Size( list_component_names );
    std::string component_names = "";
    if( num_of_component_names > 1 )
    {
        std::vector<std::string> comp_names = 
            ibe_common::IBE_Common::ComponentNamesOfVector( num_of_component_names );
        
        for( auto j = 0; j < num_of_component_names; j++ )
        {
            if( j > 0 )
            {
                component_names.append( ":" );
            }
            PyObject* component = PyList_GetItem( list_component_names, j );
            std::string component_name = PyUnicode_AsUTF8( component );
            if( component_name == "" )
            {
                component_name = comp_names.at( j );
            }
            component_names.append( component_name );
        }
    }

    // 变量值
    PyObject* key_values = PyUnicode_FromString( "values" );
    this->pyObjects_.emplace_back( key_values );
    PyObject* list_values = PyDict_GetItem( array_dict, key_values );
    int64_t num_of_values = PyList_Size( list_values );
    int64_t arraySize = num_of_values * component_size;
    float* arrayData = new float[arraySize];
    int64_t curIndex = 0; // 当前索引
    for( auto j = 0; j < num_of_values; j++ )
    {
        PyObject* component_values = PyList_GetItem( list_values, j );
        if( PyList_Check( component_values ) )
        {
            int num_of_comps = PyList_Size( component_values );
            // 分量值
            for( auto k = 0; k < num_of_comps; k++ )
            {
                PyObject* component_value = PyList_GetItem( component_values, k );
                double value = PyFloat_AsDouble( component_value );
                arrayData[curIndex] = value;
                curIndex++;
            }
        }
        else
        {
            double value = PyFloat_AsDouble( component_values );
            arrayData[curIndex] = value;
            curIndex++;
        }
    }

    array_name = ibe_common::IBE_Common::ReplaceCharOfString( array_name, ' ', '_' );

    ibe_common::IBE_FieldData* fieldInfo = new ibe_common::IBE_FieldData();
    fieldInfo->SetDataType( array_data_type );
    fieldInfo->SetFieldDataName( array_name.c_str() );
    fieldInfo->SetSubNames( component_names.c_str() );
    int fieldType = ibe_common::IBE_FieldData::IBE_FIELD_TYPE_SCALAR;
    if( component_size == 1 )
    {
        fieldType = ibe_common::IBE_FieldData::IBE_FIELD_TYPE_SCALAR;
    }
    else if( component_size == 3 )
    {
        fieldType = ibe_common::IBE_FieldData::IBE_FIELD_TYPE_VECTOR;
    }
    else // if( component_size == 6 )
    {
        fieldType = ibe_common::IBE_FieldData::IBE_FIELD_TYPE_TENSOR;
    }
    fieldInfo->SetData( fieldType, component_size, fileLocation, num_of_values, arrayData );
    delete[] arrayData;
    arrayData = nullptr;

    return fieldInfo;
}

// ----------------------------------------------------------------------------
void ibe_ansys::IBE_AnsysRstReader::Update()
{
    if( "" == this->fileName_ )
    {
        std::cerr << "IBE_AnsysRstReader: " << "The rst file must be specified!" << std::endl;
        return;
    }

    if( ibe_common::IBE_Common::HasChineseString( this->fileName_.c_str() ) == 1 ) // 路径中包含中文
    {
        // 相对路径转绝对路径
        std::string fullFilePath = ibe_common::IBE_Common::AbsolutePath( this->fileName_.c_str() );

        std::string fileName = this->GetTempFileNameOfRst();
        fileName = ibe_common::IBE_Common::AbsolutePath( fileName.c_str() );
        ibe_common::IBE_Common::CopyFileToPath( fullFilePath.c_str(), fileName.c_str() );

        // 读取Rst文件
        this->ReadAnsysRst( fileName.c_str() );
        // 移除临时文件
        remove( fileName.c_str() );
    }
    else
    {
        // 读取Rst文件
        this->ReadAnsysRst( this->fileName_.c_str() );
    }
}

// ----------------------------------------------------------------------------
void ibe_ansys::IBE_AnsysRstReader::ReadAnsysRst( const char* file_name )
{
    this->InitData();
    if( "" == file_name )
    {
        std::cerr << "IBE_AnsysRstReader: " << "The rst file must be specified!" << std::endl;
        return;
    }

    this->pyObjects_.reserve( 19 );
    this->dataInfo_ = new ibe_common::IBE_DataInformation();
    ibe_common::IBE_Meshs* meshs = this->dataInfo_->GetMeshs();
    ibe_common::IBE_Blocks* blocks = this->dataInfo_->GetBlocks();
    ibe_common::IBE_Cases* cases = this->dataInfo_->GetCases();
    try
    {
        //导入模块 
        PyObject* module = PyImport_ImportModule( "IBE_AnsysRstReader" );
        if( !module )
        {
            std::logic_error ex( "Python get module(IBE_AnsysRstReader) failed." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( module );

        // 获取类
        PyObject* ansys_rst_reader_class = PyObject_GetAttrString( module, "AnsysRstReader" );
        if( !ansys_rst_reader_class )
        {
            std::logic_error ex( "Can't find the class(AnsysRstReader)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( ansys_rst_reader_class );
        // 对类进行初始化
        // 构造参数
        PyObject* class_args = PyTuple_New( 1 );
        this->pyObjects_.emplace_back( class_args );
        PyTuple_SetItem( class_args, 0, Py_BuildValue( "s", file_name ) );

        // 构造类实例
        PyObject* ansys_rst_reader_instance = PyObject_CallObject( ansys_rst_reader_class, class_args );
        if( !ansys_rst_reader_instance )
        {
            std::logic_error ex( "Can't find the instance(AnsysRstReader)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( ansys_rst_reader_instance );

        // 调用类方法 IsLoadFile，参数分别是：类实例、方法名、参数格式、参数
        PyObject* is_Load_file_py = PyObject_CallMethod( ansys_rst_reader_instance, "IsLoadFile", "", "" );
        if( !is_Load_file_py )
        {
            std::logic_error ex( "Can't find the method(IsLoadFile)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( is_Load_file_py );
        bool is_load_file = false;
        PyArg_Parse( is_Load_file_py, "i", &is_load_file );
        if( !is_load_file )
        {
            std::logic_error ex( "Load rst file failed." );
            throw std::exception( ex );
        }

        // 点数
        PyObject* get_number_of_points_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetNumberOfPoints", "", "" );
        if( !get_number_of_points_py )
        {
            std::logic_error ex( "Can't find the method(GetNumberOfPoints)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_number_of_points_py );
        int64_t number_of_points = 0;
        PyArg_Parse( get_number_of_points_py, "i", &number_of_points );

        // 单元数
        PyObject* get_number_of_cells_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetNumberOfCells", "", "" );
        if( !get_number_of_cells_py )
        {
            std::logic_error ex( "Can't find the method(GetNumberOfCells)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_number_of_cells_py );
        int64_t number_of_cells = 0;
        PyArg_Parse( get_number_of_cells_py, "i", &number_of_cells );

        // 点坐标信息
        PyObject* get_points_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetPoints", "", "" );
        if( !get_points_py )
        {
            std::logic_error ex( "Can't find the method(GetPoints)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_points_py );
        int64_t points_size = PyList_Size( get_points_py );
        float* points = new float[points_size * 3];
        for( auto i = 0; i < points_size; i++ )
        {
            // python类型的列表元素
            // 根据下标取出python列表中的元素
            // list_item不需要释放，get_points_py释放时，list_item也被一同释放
            PyObject* list_item = PyList_GetItem( get_points_py, i );
            // 获取子元素数量
            int64_t num_of_items = PyList_Size( list_item );
            num_of_items = num_of_items > 3 ? 3 : num_of_items;
            // 遍历子元素
            for( auto j = 0; j < num_of_items; j++ )
            {
                PyObject* item = PyList_GetItem( list_item, j );
                float p = PyFloat_AsDouble( item );
                points[i * 3 + j] = p;
            }
        }
        meshs->SetPoints( points_size, points );
        delete[] points;
        points = nullptr;

        // 单元类型
        PyObject* get_cell_types_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetCellTypes", "", "" );
        if( !get_cell_types_py )
        {
            std::logic_error ex( "Can't find the method(GetCellTypes)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_cell_types_py );
        int64_t cell_types_size = PyList_Size( get_cell_types_py );
        uint8_t* cell_types = new uint8_t[cell_types_size]; // 存储所有单元的类型
        for( auto i = 0; i < cell_types_size; i++ )
        {
            // 根据下标取出python列表中的元素
            PyObject* list_item = PyList_GetItem( get_cell_types_py, i );
            cell_types[i] = PyLong_AsLong( list_item ); // 存储单元类型
        }

        // 单元构成信息
        PyObject* get_cell_connectivity_py = PyObject_CallMethod(
            ansys_rst_reader_instance, "GetCellConnectivity", "", "" );
        if( !get_cell_connectivity_py )
        {
            delete[] cell_types;
            cell_types = nullptr;

            std::logic_error ex( "Can't find the method(GetCellConnectivity)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_cell_connectivity_py );
        int64_t cell_connectivity_size = PyList_Size( get_cell_connectivity_py );
        int64_t* cellConnectivity = new int64_t[cell_connectivity_size];
        for( auto i = 0; i < cell_connectivity_size; i++ )
        {
            PyObject* list_item = PyList_GetItem( get_cell_connectivity_py, i );
            int64_t value = PyLong_AsLong( list_item );
            cellConnectivity[i] = value;
        }
        // 材料属性
        int* materials = nullptr;
        PyObject* get_materials_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetMaterials", "", "" );
        if( get_materials_py )
        {
            this->pyObjects_.emplace_back( get_materials_py );
            if( PyList_Check( get_materials_py ) )
            {
                int64_t materials_size = PyList_Size( get_materials_py );
                if( materials_size > 0 && number_of_cells > 0 )
                {
                    materials = new int[number_of_cells];
                    for( auto i = 0; i < materials_size; i++ )
                    {
                        PyObject* item = PyList_GetItem( get_materials_py, i );
                        int64_t value = PyLong_AsLong( item );
                        materials[i] = value;
                    }
                    for( auto i = materials_size; i < number_of_cells; i++ )
                    {
                        materials[i] = materials[materials_size - 1];
                    }
                }
            }
        }
        meshs->SetCells( cell_types_size, cell_types, cellConnectivity, nullptr, materials );
        if( materials )
        {
            delete[] materials;
            materials = nullptr;
        }
        delete[] cell_types;
        cell_types = nullptr;
        delete[] cellConnectivity;
        cellConnectivity = nullptr;

        // 节点网格集 
        PyObject* get_node_components_py = PyObject_CallMethod(
            ansys_rst_reader_instance, "GetNodeComponents", "", "" );
        if( get_node_components_py )
        {
            this->pyObjects_.emplace_back( get_node_components_py );

            std::vector<ibe_common::IBE_Block*> blockVector =
                this->AnalyzeComponentsFromPythonDict( get_node_components_py, 0 );
            for( auto it = blockVector.rbegin(); it != blockVector.rend(); it++ )
            {
                blocks->AddBlockToNode( *it );
            }
            std::vector<ibe_common::IBE_Block*>().swap( blockVector );
        }
        // 单元网格集 
        PyObject* get_element_components_py = PyObject_CallMethod(
            ansys_rst_reader_instance, "GetElementComponents", "", "" );
        if( get_element_components_py )
        {
            this->pyObjects_.emplace_back( get_element_components_py );

            std::vector<ibe_common::IBE_Block*> blockVector =
                this->AnalyzeComponentsFromPythonDict( get_element_components_py, 1 );
            for( auto it = blockVector.rbegin(); it != blockVector.rend(); it++ )
            {
                blocks->AddBlockToElement( *it );
            }
            std::vector<ibe_common::IBE_Block*>().swap( blockVector );
        }

        // 时间步/分析步数量
        PyObject* get_number_of_steps_py = PyObject_CallMethod( ansys_rst_reader_instance, "GetNumberOfSteps", "", "" );
        if( !get_number_of_steps_py )
        {
            std::logic_error ex( "Can't find the method(GetNumberOfSteps)." );
            throw std::exception( ex );
        }
        this->pyObjects_.emplace_back( get_number_of_steps_py );
        int64_t number_of_steps = 0;
        PyArg_Parse( get_number_of_steps_py, "i", &number_of_steps );

        // 分析步信息 
        ibe_common::IBE_Case* caseInfo = new ibe_common::IBE_Case();
        caseInfo->SetCaseName( "Case-1" ); // 默认名称
        cases->AddCase( caseInfo );

        // 随时间步/分析步变化的变量数据
        PyObject* get_attribute_data_of_steps_py = PyObject_CallMethod(
            ansys_rst_reader_instance, "GetPointDataOfSteps", "", "" );
        if( get_attribute_data_of_steps_py )
        {
            this->pyObjects_.emplace_back( get_attribute_data_of_steps_py );

            int64_t steps_size = PyList_Size( get_attribute_data_of_steps_py );
            for( auto i = 0; i < steps_size; i++ ) // 遍历时间步
            {
                PyObject* arrays_dict = PyList_GetItem( get_attribute_data_of_steps_py, i );
                if( !arrays_dict || !PyDict_Check( arrays_dict ) )
                {
                    continue;
                }

                // 时间步名
                PyObject *key_step_name = PyUnicode_FromString( "step_name" );
                this->pyObjects_.emplace_back( key_step_name );
                PyObject *dict_step_name = PyDict_GetItem( arrays_dict, key_step_name );
                std::string step_name = PyUnicode_AsUTF8( dict_step_name );

                // 时间步信息
                PyObject* key_step_information = PyUnicode_FromString( "step_information" );
                this->pyObjects_.emplace_back( key_step_information );
                PyObject* arrays_list = PyDict_GetItem( arrays_dict, key_step_information );
                if( !arrays_list || !PyList_Check( arrays_list ) )
                {
                    continue;
                }

                // 存储时间步信息
                ibe_common::IBE_CaseStep* stepInfo = new ibe_common::IBE_CaseStep();
                stepInfo->SetStepName( step_name.c_str() );
                caseInfo->AddStep( stepInfo );

                int64_t arrays_size = PyList_Size( arrays_list );
                for( auto j = 0; j < arrays_size; j++ )
                {
                    PyObject* array_dict = PyList_GetItem( arrays_list, j );
                    if( !array_dict || !PyDict_Check( array_dict ) )
                    {
                        continue;
                    }
                    ibe_common::IBE_FieldData* fieldInfo = this->AnalyzeArrayFromPythonDict( array_dict, "PointData" );
                    stepInfo->AddFieldData( fieldInfo );
                }
            }
        }

#if 0
        // 不变场数据
        /*std::vector<ibe_common::IBE_FieldData*> field_data_arrays =
            this->AnalyzeBaseArrayDataFromPythonInstance( ansys_rst_reader_instance, "FieldData" );*/
        std::vector<ibe_common::IBE_FieldData*> point_data_arrays =
            this->AnalyzeBaseArrayDataFromPythonInstance( ansys_rst_reader_instance, "PointData" );
        std::vector<ibe_common::IBE_FieldData*> cell_data_arrays =
            this->AnalyzeBaseArrayDataFromPythonInstance( ansys_rst_reader_instance, "CellData" );

        // 每个时间步中都添加不变场数据
        int steps_num = caseInfo->GetNumberOfSteps();
        for( auto i = 0; i < steps_num; i++ )
        {
            ibe_common::IBE_CaseStep* stepInfo = caseInfo->GetStep( i );
            for( auto it = point_data_arrays.rbegin(); it != point_data_arrays.rend(); it++ )
            {
                ibe_common::IBE_FieldData* fieldInfo = new ibe_common::IBE_FieldData( *it );
                stepInfo->AddFieldData( fieldInfo );
            }
            for( auto it = cell_data_arrays.rbegin(); it != cell_data_arrays.rend(); it++ )
            {
                ibe_common::IBE_FieldData* fieldInfo = new ibe_common::IBE_FieldData( *it );
                stepInfo->AddFieldData( fieldInfo );
            }
        }

        // 释放空间
        for( auto it = point_data_arrays.rbegin(); it != point_data_arrays.rend(); it++ )
        {
            delete *it;
        }
        std::vector<ibe_common::IBE_FieldData*>().swap( point_data_arrays );
        for( auto it = cell_data_arrays.rbegin(); it != cell_data_arrays.rend(); it++ )
        {
            delete *it;
        }
        std::vector<ibe_common::IBE_FieldData*>().swap( cell_data_arrays );
#endif // 0

        // 释放python空间
        this->FreePyObjects();
    }
    catch( const std::exception &ex )
    {
        this->InitData();
        std::cerr << "IBE_AnsysRstReader: " << ex.what() << std::endl;
        if( PyErr_Occurred() )
        {
            PyErr_Print();
        }
    }
}

// ----------------------------------------------------------------------------
std::string ibe_ansys::IBE_AnsysRstReader::GetTempFileNameOfRst()
{
    // 存储临时文件的文件夹
    std::string tempFolderPath = "./Temp";
    // 判断文件是否存在:0-存在，-1-不存在 
    if( access( tempFolderPath.c_str(), 0 ) == -1 )
    {
        // 创建文件夹
        mkdir( tempFolderPath.c_str() );
    }

    // 复制后的临时文件文件路径
    time_t nowtime; // 当前时间，精确到秒
    time( &nowtime );
    std::string fileName = tempFolderPath + "/" + std::to_string( nowtime ) + ".rst";
    if( access( fileName.c_str(), 0 ) == 0 )
    {
        int i = 0;
        while( true )
        {
            std::string tempFileName =
                tempFolderPath + "/" + std::to_string( nowtime ) + std::to_string( i ) + ".rst";
            if( access( tempFileName.c_str(), 0 ) != 0 ) // 判断文件是否存在:0-存在，-1-不存在 
            {
                fileName = tempFileName;
                break;
            }
            i++;
        }
    }
    return fileName;
}

// ----------------------------------------------------------------------------
std::vector<ibe_common::IBE_Block*> ibe_ansys::IBE_AnsysRstReader::AnalyzeComponentsFromPythonDict(
    PyObject * components_list, const int type )
{
    std::vector<ibe_common::IBE_Block*> blockVector;
    if( !components_list || !PyList_Check( components_list ) )
    {
        return blockVector;
    }

    int blockDataType = type == 1 ?
        ibe_common::IBE_Block::IBE_BLOCK_ELEMENT_SET : ibe_common::IBE_Block::IBE_BLOCK_POINT_SET;

    int64_t components_size = PyList_Size( components_list );
    blockVector.reserve( components_size );
    for( auto i = 0; i < components_size; i++ ) // 遍历网格集
    {
        PyObject* components_dict = PyList_GetItem( components_list, i );
        if( !components_dict || !PyDict_Check( components_dict ) )
        {
            continue;
        }

        // 网格集名 
        PyObject *key_components_name = PyUnicode_FromString( "name" );
        this->pyObjects_.emplace_back( key_components_name );
        PyObject *dict_components_name = PyDict_GetItem( components_dict, key_components_name );
        std::string components_name = PyUnicode_AsUTF8( dict_components_name );
        // 网格集内容 
        PyObject* key_components_values = PyUnicode_FromString( "values" );
        this->pyObjects_.emplace_back( key_components_values );
        PyObject* values_list = PyDict_GetItem( components_dict, key_components_values );
        int64_t values_size = 0;
        int64_t* components_values = nullptr;
        if( values_list && PyList_Check( values_list ) )
        {
            values_size = PyList_Size( values_list );
            if( values_size > 0 )
            {
                components_values = new int64_t[values_size];
                for( auto j = 0; j < values_size; j++ )
                {
                    PyObject* item = PyList_GetItem( values_list, j );
                    int64_t value = PyLong_AsLong( item );
                    components_values[j] = value;
                }
            }
        }
        // 存储网格集信息
        components_name = ibe_common::IBE_Common::ReplaceCharOfString( components_name, ' ', '_' );
        ibe_common::IBE_Block* blockInfo = new ibe_common::IBE_Block();
        blockInfo->SetBlockName( components_name.c_str() );
        blockInfo->SetBlockData( blockDataType, values_size, components_values );
        blockVector.emplace_back( blockInfo );
        if( components_values )
        {
            delete[] components_values;
            components_values = nullptr;
        }
    }

    return blockVector;
}

// ----------------------------------------------------------------------------
void ibe_ansys::IBE_AnsysRstReader::FreePyObjects()
{
    // Py_XDECREF和Py_DECREF功能相同，但可以处理空指针
    for( auto it = this->pyObjects_.rbegin(); it != this->pyObjects_.rend(); it++ )
    {
        Py_XDECREF( *it ); // 释放python空间
    }
    std::vector<PyObject*>().swap( this->pyObjects_ );
}
